As promised, I wrote and released Text-Sprintf-Named onto the CPAN. This is an implementation of a Python-like sprintf with named conversions. So you can do my $formatter = Text::Sprintf::Named->new({fmt => 'Hello %(name)s! You have $%(money)04d in your bank account!'); and then $formatter->format({args => {name => "Joey", money => 350}}) and get 'Hello Joey! You have $0350 in your bank account!' in return.
Now that I look at its POD again I see that the code in the synopsis is wrong. I guess I'll have to release version 0.01001 soonishly. If you find this module useful, that's good, and if not, I hope I haven't made your situation any worse. I wrote it because I thought it may prove useful for a Test::Run plugin, so necessity was the mother of implementation in this case.
Cheers all, and a Happy Thanksgiving to all Americans.
%2$.3fs
to format the second parameter (the part before the "$") as a float with 3 decimals.
Re:Text vs. String
Shlomi Fish on 2006-11-24T04:45:18
I see, by the acknowledgements in your docs, that you've seen my module, String::Sprintf. I'm wondering why you chose the name Text::Sprintf::Named instead of String::Sprintf::NamedWhen I first came up with the idea of having a sprintf with named conversions, I wanted to call the module Text-Sprintf-Named. That was before I learned about your efforts, and when I implemented it, I followed my initial mental inertia.
A bit of consistency among module authors would be nice.To quote Larry Wall: "The problem with being consistent is that there are lots of ways to be consistent, and they're all inconsistent with each other.". Seriously now, Text::Sprintf::Named can optionally make use of String::Sprintf or any other customised sprintf by over-riding its _sprintf method. Note that at the moment every conversion is rendered separately, but I'd like to fix this problem sometime.
Re:Text vs. String
bart on 2006-11-24T20:08:46
When I first came up with the idea of having a sprintf with named conversions, I wanted to call the module Text-Sprintf-Named. That was before I learned about your efforts, and when I implemented it, I followed my initial mental inertia.Oh, OK. Your reasons are as good as mine, so I will not say you're wrong. Plus, if you name your module after mine, it would seem like yours is based on mine, while it is not.
Anyway, now that I know it exists, I'll keep an eye on it, and possibly we can try to synchronize them, so they work well together, if that looks desirable.
Re:Text vs. String
Shlomi Fish on 2006-11-24T21:19:16
Plus, if you name your module after mine, it would seem like yours is based on mine, while it is not.Indeed. This is one reason I believe String-Sprintf is not a good enough name for the module you wrote, because there may be other String-Sprintf-* modules, with different takes on an enhanced sprintf-like functionality. I used *-Sprintf-Named to indicate something on the enhancement I intended the module to have.
Anyway, now that I know it exists, I'll keep an eye on it, and possibly we can try to synchronize them, so they work well together, if that looks desirable.Like I said in my previous reply upthread, it should be doable.